<?php

namespace App\Models;

use App\Services\CommonService;
use App\Services\GoodsService;
use App\Services\OrderService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderLeaseAudit extends Model
{
    use HasFactory;
    protected $table = 'sp_order_lease_audit';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //账单状态
    const CHECK_STATUS_NOAUDIT = 0;//待审核
    const CHECK_STATUS_AUDIT = 1;//已审核
    const CHECK_STATUS_REJECT = 2;//已拒绝



    const CHECK_STATUS_LIST = [
        '0' => '待审核',
        '1' => '已审核',
        '2' => '已拒绝',
    ];

    public function getCheckStatusAttribute($value){
        return ['text'=>self::CHECK_STATUS_LIST[$value],'value'=>$value];
    }
//    public function getCreateTimeAttribute($value){
//        return $value?date('Y-m-d h:i:s',$value):'';
//    }

    //审核列表
    public  function checkList($request){
        $pageSize = !empty($request['pageSize'])?$request['pageSize']:CommonService::PAGESIZE;
        $where=[];
        $check_where=[];

        if(!empty($request['trade_no'])){
            $where[] = ['o.trade_no','like','%'.trim($request['trade_no']).'%'];
        }
        if(!empty($request['mobile'])){
            $where[] = ['m.mobile','=',trim($request['mobile'])];
        }
        if(!empty($request['name'])){
            $where[] = ['m.cert_name','=',trim($request['name'])];
        }
        if($request['check_status']!=''){
            $check_where[] = ['g.check_status',$request['check_status']];
        }
        if(!empty($request['create_time'])){
            $time_data = searchTime($request['create_time']);
            $begin_time = $time_data['begin_time'];
            $end_time = $time_data['end_time'];
            $where[] = ['g.create_time','>=',strtotime($begin_time)];
            $where[] = ['g.create_time','<=',strtotime($end_time)];
        }
//        $where[] = ['g.is_delete',GoodsService::GOODS_NO_DELETE_STATUS];

        $list = $this->from('sp_order_lease_audit as g')->where($where)->where($check_where)
            ->leftjoin('sp_order_2 as o','o.id','=','g.order_id')
            ->leftjoin('sp_member_2 as m','m.id','=','o.uid')
            ->leftjoin('sp_other_admin as ad','ad.id','=','g.admin_id')
            ->leftjoin('sp_other_admin as ado','ado.id','=','g.operator_id')
            ->leftjoin('sp_order_lease_2 as c','c.id','=','g.lease_id')
            ->select('g.id','o.trade_no','m.mobile',
                'm.cert_name','c.which_period',
                'c.rent','c.status','c.pay_time',
                'g.create_time','g.check_status','g.sub_remark','g.admin_id','g.transaction_id',
                'g.operator_id','g.image_path','c.pay_type','c.begin_time','ad.username','ado.username as ousername')
            ->orderBy('g.id', 'desc')
            ->paginate($pageSize);
        foreach ($list as $ra){
            $ra['pay_time']=$ra['pay_time']?date('Y-m-d H:i:s',$ra['pay_time']):'';
            $ra['create_time']=$ra['create_time']?date('Y-m-d H:i:s',$ra['create_time']):'';
        }
        unset($check_where);
        $check_where[] = ['g.check_status','>=',self::CHECK_STATUS_NOAUDIT];
        $all_audit = $this->from('sp_order_lease_audit as g')
            ->leftjoin('sp_order_2 as o','o.id','=','g.order_id')
            ->leftjoin('sp_member_2 as m','m.id','=','o.uid')
            ->leftjoin('sp_order_lease_2 as c','c.id','=','g.lease_id')
            ->where($where)->where($check_where)->count();

        unset($check_where);
        $check_where[] = ['g.check_status',self::CHECK_STATUS_NOAUDIT];
        $nocheck_count = $this->from('sp_order_lease_audit as g')
            ->leftjoin('sp_order_2 as o','o.id','=','g.order_id')
            ->leftjoin('sp_member_2 as m','m.id','=','o.uid')
            ->leftjoin('sp_order_lease_2 as c','c.id','=','g.lease_id')->where($where)->where($check_where)->count();
        unset($check_where);
        $check_where[] = ['g.check_status',self::CHECK_STATUS_AUDIT];
        $checked_count = $this->from('sp_order_lease_audit as g')->where($where)->where($check_where)
            ->leftjoin('sp_order_2 as o','o.id','=','g.order_id')
            ->leftjoin('sp_member_2 as m','m.id','=','o.uid')
            ->leftjoin('sp_order_lease_2 as c','c.id','=','g.lease_id')->count();
        unset($check_where);
        $check_where[] = ['g.check_status',self::CHECK_STATUS_REJECT];
        $refuse_count = $this->from('sp_order_lease_audit as g')->where($where)->where($check_where)
            ->leftjoin('sp_order_2 as o','o.id','=','g.order_id')
            ->leftjoin('sp_member_2 as m','m.id','=','o.uid')
            ->leftjoin('sp_order_lease_2 as c','c.id','=','g.lease_id')->count();

        $list = $list ? $list->toArray() : array();
        $list['count']['all_audit'] = $all_audit;
        $list['count']['nocheck_count'] = $nocheck_count;
        $list['count']['checked_count'] = $checked_count;
        $list['count']['refuse_count'] = $refuse_count;
        return $list;
    }

}
